﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCSuaMon : UserControl
    {
        MonHocBUO mhBUO = new MonHocBUO();

        int id;
        String id_Mon;

        public UCSuaMon(String idKhoi, String idMon)
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = mhBUO.HienThiMon();
            showKhoi();
            lbl_IDMon.Text = idMon;
            txtTenMon.Text = mhBUO.getMonHoc(idMon);
            cbbKhoi.Text = mhBUO.getKhoi(idKhoi);
            
        }

        private void showKhoi()
        {
            tbl_Khoi tbl_Khoi = (tbl_Khoi)cbbKhoi.SelectedItem;
            cbbKhoi.DataSource = mhBUO.getKhoi();
            cbbKhoi.DisplayMember = "TenKhoi";
            cbbKhoi.ValueMember = "id_Khoi";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if ((txtTenMon.Text == "") || (cbbKhoi.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                tbl_MonHoc tbl_mh = new tbl_MonHoc();
                tbl_mh.id_Khoi = cbbKhoi.SelectedValue.ToString();
                tbl_mh.TenMonHoc = txtTenMon.Text;
                if (mhBUO.Update(lbl_IDMon.Text, tbl_mh))
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = mhBUO.HienThiMon();
                    MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Cập nhật thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            dataGridView1.Enabled = true;
            binding();
        }

        public void binding()
        {
            lbl_IDMon.DataBindings.Clear();
            lbl_IDMon.DataBindings.Add("Text", dataGridView1.DataSource, "id_MonHoc");
            txtTenMon.DataBindings.Clear();
            txtTenMon.DataBindings.Add("Text", dataGridView1.DataSource, "TenMonHoc");
            cbbKhoi.DataBindings.Clear();
            cbbKhoi.DataBindings.Add("Text", dataGridView1.DataSource, "TenKhoi");
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_Mon = dataGridView1.Rows[id].Cells[0].Value.ToString();
                        

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void danhSáchMônHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachMon a = new UCDanhSachMon();
            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void thêmMônHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemMon a = new UCThemMon();
            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void xóaMônHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu giáo viên", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (mhBUO.Delete(id_Mon))
                {
                    MessageBox.Show("Xóa môn thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa môn thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = mhBUO.HienThiMon();
            }
        }
    }
}
